package cn.flying.base.sys.configuration.shiro;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import java.io.IOException;

import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

/**
 * @description: 登录拦截
 * @author: lvyong
 * @date: 2022年10月25日 23:48
 * @version: 1.0
 */
public class ShiroLoginFilter extends FormAuthenticationFilter {

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        if (request instanceof HttpServletRequest) {

        }
        return super.onAccessDenied(request, response);
    }

    /**
     * 如果返回false，则执行onAccessDenied
     * @param request
     * @param response
     * @param mappedValue
     * @return
     */
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
//        if (request instanceof HttpServletRequest) {
//            if (((HttpServletRequest) request).getMethod().toUpperCase().equals("OPTIONS")) {
//                return true;
//            }
//        }
        HttpServletRequest req = (HttpServletRequest) request;
        System.out.println((req).getMethod().toUpperCase());
        System.out.println(req.getRequestURI());
        System.out.println(isLoginRequest(request, response));
        System.out.println(isLoginSubmission(request, response));
        return super.isAccessAllowed(request, response, mappedValue);
    }

    @Override
    public void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        super.doFilterInternal(request, response, chain);
    }
}
